﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Mail;
using System.Configuration;

namespace Ebiz.libs
{
    public enum EmailEngine{
        CDOSYS, NETMAIL
    }
    public enum ConfigSource { 
        DATABASE, WEBCONFIG
    }
    public class CLSMail{

        public enum MessageMode{
            REQUEST, APPROVED, RETURNED, REJECTED
        }
        public static string GenerateMessage(Dictionary<string, string> MailDetails,
            MessageMode MessageType){
            string Requester = MailDetails["Requester"];
            string AppvPerson = MailDetails["AppvPerson"];
            string AppvPersonExt = MailDetails["AppvPersonExt"];
            string CLNum = MailDetails["CLNum"];
            string PageLink = MailDetails["Link"];

            string result = "<html>";
            result += "<head></head>";
            result += "<body>";

            switch (MessageType){
                case MessageMode.REQUEST:
                    result += "<span>Dear " + AppvPerson + ",</span>";
                    result += "<p>" + Requester + " ";
                    result += "has requested a sales discount program through Ebiz System ";
                    result += "with CL Number <b>";
                    result += CLNum + "</b>, ";
                    result += "and await for your review and approval on this request.</p>";
                    result += "<p>Please click <a href=\"" + PageLink + "\"><b>here</b></a> ";
                    result += "to review and approve the request.</p>";
                    break;
                case MessageMode.APPROVED:
                    result += "<span>Dear " + Requester + ",</span>";
                    result += "<p>Your BizCase request with CL Number ";
                    result += CLNum + " has been <b>APPROVED</b></p>";
                    result += "<p>Please click <a href=\"" + PageLink + "\"><b>here</b></a> ";
                    result += "to view the request.</p>";
                    break;
                case MessageMode.RETURNED:
                    result += "<span>Dear " + Requester + ",</span>";
                    result += "<p>Your BizCase request with CL Number ";
                    result += CLNum + " has been <b>RETURNED FOR INFORMATION</b></p>";
                    result += "<p>Please click <a href=\"" + PageLink + "\"><b>here</b></a> ";
                    result += "to view the request.</p>";
                    break;
                case MessageMode.REJECTED:
                    result += "<span>Dear " + Requester + ",</span>";
                    result += "<p>Your BizCase request with CL Number ";
                    result += CLNum + " has been <b>REJECTED</b></p>";
                    result += "<p>Please click <a href=\"" + PageLink + "\"><b>here</b></a> ";
                    result += "to view the request.</p>";
                    break;
            }
            result += "Thank you and best regards,";
            result += "<p><b>Ebiz Admin</b></p>";
            result += "</body>";
            result += "</html>";
            return result;
        }

        private static Dictionary<string, object> SendNow(Dictionary<string, string> EmailContents, 
            EmailEngine Engine) {
            Dictionary<string, object> result = new Dictionary<string, object>();
            
            MailMessage Mailer = new MailMessage();
            Mailer.From = new MailAddress("eBiz@astrazeneca.com", "Ebiz Admin");
            Mailer.To.Add("akangcupez@gmail.com");
            Mailer.CC.Add("zenislev2801@gmail.com");
            Mailer.Priority = MailPriority.High;
            Mailer.IsBodyHtml = true;
            Mailer.Body = "";

            SmtpClient _SMTP = new SmtpClient();
            _SMTP.EnableSsl = true;
            _SMTP.Timeout = 60000;
            try{
                _SMTP.Send(Mailer);
                result["Status"] = "1";
                result["Error"] = "";
            }
            catch (Exception ex){
                string ErrMsg = ex.Message;
                result["Status"] = "0";
                result["Error"] = ErrMsg;
            }
            return result;
        }

    }
}
